﻿using Ldt.DataEngine.DataFlowParse;
using Ldt.DataEngine.DataFlowParse.Model;
using Ldt.DataEngine.LogHandle;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.CommModel;
using System;
using System.Collections.Generic;
using System.Linq;
using static Ldt.DataEngine.LogHandle.LogHandle;

namespace Ldt.DataEngin.Assemble
{
    /// <summary>
    /// 上层入口的基础对象
    /// </summary>
    public class MainComponentBase : IMainComponent
    {
        #region 基础信息
        //private LogShow _uishow;
        private LogShowFontStyle _uilogShowFontStyle;
        public List<ConfigModel> configmodels = new List<ConfigModel>();
        ///// <summary>
        ///// 记录日志
        ///// </summary>
        ///// <param name="logType">消息类型</param>
        ///// <param name="text">日志内容</param>
        //private void DoSetLogtype(string text, LogMessageType logType = LogMessageType.Warn)
        //{
        //    LogHType log = LogHType.Info;
        //    foreach (LogHType typ in Enum.GetValues(typeof(LogHType)))
        //    {
        //        if (((int)logType) == ((int)typ))
        //        {
        //            log = typ;
        //            break;
        //        }
        //    }
        //    //写入记录日志
        //    LogHandle.GetInitLogHandle.LogWrite(log, text);
        //    if (_uishow != null)
        //    {
        //        this._uishow(text, logType);
        //    }


        //}

        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="logText">日志内容</param>
        /// <param name="fontStyle">样式</param>
        /// <param name="logType">日志输出等级</param>
        private void DoSetLogStyle(string logText, FontStyles fontStyle, LogMessageType logType)
        {
            LogHType log = LogHType.Info;
            foreach (LogHType typ in Enum.GetValues(typeof(LogHType)))
            {
                if (((int)logType) == ((int)typ))
                {
                    log = typ;
                    break;
                }
            }
            //写入记录日志
            LogHandle.GetInitLogHandle.LogWrite(log, logText);
            if (_uilogShowFontStyle != null)
            {
                this._uilogShowFontStyle(logText, fontStyle);
            }


        }

        /// <summary>
        /// 日志显示
        /// </summary>
        public LogShow LogShow { get; set; }
        //{
        //    get { return DoSetLogtype; }
        //    set { _uishow = value; }
        //}

        /// <summary>
        /// 显示带有样式的日志
        /// </summary>
        public LogShowFontStyle LogShowFontStyle
        {
            get { return DoSetLogStyle; }
            set { _uilogShowFontStyle = value; }
        }

        /// <summary>
        /// 总体进度显示
        /// </summary>
        public ProcessShow ProcessShow { get; set; }

        /// <summary>
        /// 模块进度显示
        /// </summary>
        public ModuleProcessShow ModuleProcessShow { get; set; }

        /// <summary>
        /// XML配置信息
        /// </summary>
        public string XmlMessage { get; set; }

        /// <summary>
        /// 进度信息
        /// </summary>
        public InteractiveShowClass InteractiveShow { get; private set; }

        /// <summary>
        /// 分析结束标记
        /// </summary>
        public FinishShow Finishow { get; set; }

        /// <summary>
        /// 输入参数
        /// </summary>
        public MainInputParameterModel inputParameter;

        /// <summary>
        /// 上下文
        /// </summary>
        public DataContext dataContext;

        #endregion
        private readonly ConfigFlowLoad _flowLoad;

        public MainComponentBase(ConfigFlowLoad flowLoad)
        {
            _flowLoad = flowLoad;
        }

        /// <summary>
        /// 模块执行进度
        /// </summary>
        /// <param name="moduleName">模块名称</param>
        /// <param name="nvalue">进度值</param>
        public void SetModuleProcess(string moduleName, int nvalue, MainInputParameterModel inputParameterModel)
        {
            ModuleProcessShow?.Invoke(moduleName, nvalue, inputParameterModel);
        }

        public void SetModularModel(string message, string type)
        {
            //ModularModel?.Invoke(message, type);
        }

        /// <summary>
        /// 拉取流程配置
        /// </summary>
        /// <param name="flowType"></param>
        /// <returns></returns>
        public List<ConfigModel> GetFlowConfig(ConfigFlowLoad.FlowType flowType)
        {
            RequestPara requ = new RequestPara()
            {
                CaseType = inputParameter.CaseType,
                CaseCause = inputParameter.CaseCause,
                AppId = inputParameter.AppId,
                CourtId = inputParameter.CourtId,
                DocID = inputParameter.DocID
            };
            configmodels = _flowLoad.LoadFlowInfo(requ, flowType);
            return configmodels;
        }


        /// <summary>
        /// 拉取流程配置
        /// </summary>
        /// <param name="flowType"></param>
        /// <returns></returns>
        public List<ConfigModel> GetFlowConfig(ConfigFlowLoad.FlowType flowType, string doctype)
        {
            RequestPara requ = new RequestPara()
            {
                CaseType = inputParameter.CaseType,
                CaseCause = inputParameter.CaseCause,
                AppId = inputParameter.AppId,
                CourtId = inputParameter.CourtId,
                DocID = doctype
            };
            configmodels = _flowLoad.LoadFlowInfo(requ, flowType);
            return configmodels;
        }

        /// <summary>
        /// 数据分析
        /// </summary>
        public void ExtractWrap()
        {
            inputParameter.configModelList = configmodels;
            if (dataContext == null)
            {
                MainBaseNew mainBaseNew = new MainBaseNew(inputParameter);
                //全部进行一开始就实例化
                string backXml = string.Empty;
                if (!string.IsNullOrWhiteSpace(inputParameter.BCXml))
                    backXml = HttpHelp.DeCodeBase64new(65001, inputParameter.BCXml);
                mainBaseNew.dataContext = new DataContext(new DataLayerSysStores(), new DyData_Analysis(), new InteractiveShowClass());
                ConfigModel processmo = configmodels.FirstOrDefault(a => a.Code == "ProcessConfig");
                mainBaseNew.dataContext.DataLayerSys.SetConfigModel(processmo.Code, processmo);
                mainBaseNew.dataContext.DataLayerAns.CaseType = inputParameter.CaseType;
                mainBaseNew.dataContext.DataLayerAns.SatgeAnalysis = inputParameter.laststage;
                mainBaseNew.dataContext.DataLayerAns.BaseElement = inputParameter.BaseElement;
                mainBaseNew.dataContext.DataLayerAns.MergeElment = inputParameter.MergeElement;
                mainBaseNew.dataContext.DataLayerAns.dicTable = inputParameter.dicTabel;
                mainBaseNew.dataContext.DataLayerAns.BcXML = backXml;
                mainBaseNew.dataContext.DataLayerAns.District = inputParameter.District;
                mainBaseNew.dataContext.InteractiveShow.inputParameterModel = inputParameter;
                if (mainBaseNew.dataContext.InteractiveShow.LogShow == null)
                    mainBaseNew.dataContext.InteractiveShow.LogShow = new LogShow(LogShow);//日志显示
                if (mainBaseNew.dataContext.InteractiveShow.Finishow == null)
                    mainBaseNew.dataContext.InteractiveShow.Finishow = Finishow;//结束分析
                if (mainBaseNew.dataContext.InteractiveShow.ProcessShow == null)
                    mainBaseNew.dataContext.InteractiveShow.ProcessShow = new ProcessShow(SetProgress);//进度显示
                if (mainBaseNew.dataContext.InteractiveShow.ModuleProcessShow == null)
                    mainBaseNew.dataContext.InteractiveShow.ModuleProcessShow = ModuleProcessShow;//进度显示
                mainBaseNew.Optimization();
                mainBaseNew.Extract(configmodels.Where(a => a.Code != "ProcessConfig").ToList());
                dataContext = mainBaseNew.dataContext;
            }
            else
            {
                MainBaseNew mainBaseNew = new MainBaseNew(inputParameter);
                //全部进行一开始就实例化
                mainBaseNew.dataContext = dataContext;
                ConfigModel processmo = configmodels.FirstOrDefault(a => a.Code == "ProcessConfig");
                mainBaseNew.dataContext.DataLayerSys.SetConfigModel(processmo.Code, processmo);
                mainBaseNew.dataContext.DataLayerAns.CaseType = inputParameter.CaseType;
                mainBaseNew.dataContext.DataLayerAns.SatgeAnalysis = inputParameter.laststage;
                mainBaseNew.dataContext.DataLayerAns.BaseElement = inputParameter.BaseElement;
                mainBaseNew.dataContext.DataLayerAns.MergeElment = inputParameter.MergeElement;
                mainBaseNew.dataContext.DataLayerAns.District = inputParameter.District;
                mainBaseNew.dataContext.DataLayerAns.dicTable = inputParameter.dicTabel;
                if (mainBaseNew.dataContext.InteractiveShow.LogShow == null)
                    mainBaseNew.dataContext.InteractiveShow.LogShow = LogShow;//日志显示
                if (mainBaseNew.dataContext.InteractiveShow.Finishow == null)
                    mainBaseNew.dataContext.InteractiveShow.Finishow = Finishow;//结束分析
                if (mainBaseNew.dataContext.InteractiveShow.ProcessShow == null)
                    mainBaseNew.dataContext.InteractiveShow.ProcessShow = new ProcessShow(SetProgress);//进度显示
                if (mainBaseNew.dataContext.InteractiveShow.ModuleProcessShow == null)
                    mainBaseNew.dataContext.InteractiveShow.ModuleProcessShow = ModuleProcessShow;//进度显示
                mainBaseNew.Optimization();
                mainBaseNew.Extract(configmodels.Where(a => a.Code != "ProcessConfig").ToList());
                dataContext = mainBaseNew.dataContext;
            }

        }

        /// <summary>
        /// 数据优选
        /// </summary>
        public void OptimizationWrap()
        {
          
            MainBaseNew mainBaseNew = new MainBaseNew(inputParameter);
            mainBaseNew.dataContext = new DataContext(dataContext.DataLayerSys,dataContext.DataLayerAns,new DyData_Optimize(),dataContext.InteractiveShow);
            mainBaseNew.dataContext.DataLayerAns.SatgeAnalysis = inputParameter.laststage;
            ConfigModel processmo = configmodels.FirstOrDefault(a => a.Code == "ProcessConfig");
            mainBaseNew.dataContext.DataLayerSys.SetConfigModel(processmo.Code, processmo);
            if (mainBaseNew.dataContext.InteractiveShow.LogShow == null)
                mainBaseNew.dataContext.InteractiveShow.LogShow = LogShow;//日志显示
            if (mainBaseNew.dataContext.InteractiveShow.Finishow == null)
                mainBaseNew.dataContext.InteractiveShow.Finishow = Finishow;//结束分析
            if (mainBaseNew.dataContext.InteractiveShow.ProcessShow == null)
                mainBaseNew.dataContext.InteractiveShow.ProcessShow = new ProcessShow(SetProgress);//进度显示
            if (mainBaseNew.dataContext.InteractiveShow.ModuleProcessShow == null)
                mainBaseNew.dataContext.InteractiveShow.ModuleProcessShow = ModuleProcessShow;//进度显示
            //mainBaseNew.Optimization(configmodels.Where(a=>a.Code!="ProcessConfig").ToList());
            dataContext = mainBaseNew.dataContext;
        }

        /// <summary>
        /// 进度显示
        /// </summary>
        /// <param name="fileId">文件ID</param>
        /// <param name="nvalue">进度值</param>
        public void SetProgress(string fileId, int nvalue, MainInputParameterModel inputParameterModel)
        {
            ProcessShow?.Invoke(fileId, nvalue, inputParameterModel);
        }
    }
}
